package com.sky.financelake.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;

import com.sky.financelake.R;
import com.sky.financelake.bean.dto.StockDto;

import java.util.List;

public class StockAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final String TAG = StockAdapter.class.getCanonicalName();

    private final List<StockDto> stockDtos;
    private final Context mContext;

    public StockAdapter(List<StockDto> stockDtos, Context mContext) {
        this.stockDtos = stockDtos;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_stock, parent, false);
        return new StockViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof StockViewHolder) {
            ((StockViewHolder) holder).name.setText(stockDtos.get(position).getName());
            ((StockViewHolder) holder).code.setText(stockDtos.get(position).getSymbol());
            ((StockViewHolder) holder).industry.setText(stockDtos.get(position).getIndustry());
            ((StockViewHolder) holder).market.setText(stockDtos.get(position).getMarket());
        }
    }

    @Override
    public int getItemCount() {
        return stockDtos.size();
    }

    public interface OnItemClickListener{
        void onClick(int position);
    }
    public OnItemClickListener listener;
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.listener = onItemClickListener;
    }

    public class StockViewHolder extends RecyclerView.ViewHolder {

        AppCompatTextView name, code, industry, market;

        public StockViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
            code = itemView.findViewById(R.id.code);
            industry = itemView.findViewById(R.id.industry);
            market = itemView.findViewById(R.id.market);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener!=null){
                        listener.onClick(getAdapterPosition());
                    }
                }
            });
        }
    }
}
